package com.li.configserve.config.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.li.configserve.domain.properties.MysqlProperties;
import com.li.toolkit.constant.LogConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description：Druid数据库连接池配置
 * @Author：LiDong
 * @Create：2020/8/16
 * @Version：1.0.0
 */
@Configuration
public class DruidConfig {

    private final Logger logger= LoggerFactory.getLogger(DruidConfig.class);

    @Autowired
    MysqlProperties mysqlProperties;


    /*
     @Bean
     public DruidDataSource getDruiddatasource(){
         try{
             logger.info("Druid初始化开始==========");
             DruidDataSource druidDataSource=new DruidDataSource();
             druidDataSource.setUrl(mysqlProperties.getJdbcurl());
             druidDataSource.setUsername(mysqlProperties.getUsername());
             druidDataSource.setPassword(mysqlProperties.getPassword());
             druidDataSource.setMaxActive(mysqlProperties.getMaximumPoolSize());
             druidDataSource.setDriverClassName(mysqlProperties.getDrivername());
             DruidPooledConnection connection = druidDataSource.getConnection();
             logger.info("Druid初始化结束==========");
             return druidDataSource;
         }catch (Exception e){
             e.printStackTrace();
             return null;
         }
     }
    */

    /**
     * 初始化
     */
    @Bean(destroyMethod = "close", initMethod = "init")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource druidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        logger.info(LogConst.LOG_SUCCESS_PREFIX+"Druid初始化完成...");
        return druidDataSource;
    }


    @Bean
    public ServletRegistrationBean<StatViewServlet> druidServlet(){
        ServletRegistrationBean<StatViewServlet> servletRegistartionBean= new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        // 白名单,多个用逗号分割， 如果allow没有配置或者为空，则允许所有访问
        servletRegistartionBean.addInitParameter("allow", "127.0.0.1,172.29.32.54");
        // 黑名单,多个用逗号分割 (共同存在时，deny优先于allow)
        servletRegistartionBean.addInitParameter("deny", "192.168.1.110");
        // 控制台管理用户名
        servletRegistartionBean.addInitParameter("loginUsername", "admin");
        // 控制台管理密码
        servletRegistartionBean.addInitParameter("loginPassword", "admin");
        // 是否可以重置数据源，禁用HTML页面上的“Reset All”功能
        servletRegistartionBean.addInitParameter("resetEnable", "false");
        return servletRegistartionBean;
    }

    /**
     * 定义Druid过滤规则
     */
    @Bean
    public FilterRegistrationBean<WebStatFilter> filterRegistrationBean() {
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>() ;
        filterRegistrationBean.setFilter(new WebStatFilter());
        //所有请求进行监控处理
        filterRegistrationBean.addUrlPatterns("/*");
        //添加不需要忽略的格式信息
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*");
        return filterRegistrationBean ;
    }
}
